package com.fa4j.product.comment.controller;


import com.fa4j.common.base.exception.CommonRequestError;
import com.fa4j.common.base.model.Page;
import com.fa4j.common.base.model.RemoveCmd;
import com.fa4j.common.web.annotation.ApiMethod;
import com.fa4j.common.web.model.AppType;
import com.fa4j.product.comment.model.Comment;
import com.fa4j.product.comment.model.CommentCreateCmd;
import com.fa4j.product.comment.model.CommentPageQuery;
import com.fa4j.product.comment.repository.CommentRepository;
import com.fa4j.product.comment.service.CommentService;
import com.fa4j.system.login.util.LoginUtil;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Web接口-商品评论
 *
 * @since 2025-11-22 21:06:24
 */
@RestController
@RequestMapping("product/comment")
@RequiredArgsConstructor
public class CommentController {

    private final CommentService commentService;
    private final CommentRepository commentRepository;


    @GetMapping("page")
    @ApiMethod(name = "商品评论-分页查询", type = AppType.MANAGE)
    public Page<Comment> page(CommentPageQuery query) {
        return commentRepository.queryPage(query);
    }

    @GetMapping("{id}")
    @ApiMethod(name = "商品评论-详情查询", type = AppType.MANAGE)
    public Comment detail(@PathVariable Long id) {
        return commentRepository.findById(id)
                .orElseThrow(() -> CommonRequestError.NOT_FOUND.build("商品评论"));
    }

    @DeleteMapping("{id}")
    @ApiMethod(name = "商品评论-删除", type = AppType.MANAGE)
    public Comment remove(@PathVariable Long id) {
        return commentService.remove(RemoveCmd.of(id, LoginUtil.getAccountId()));
    }

    @PostMapping
    @ApiMethod(name = "商品评论-提交", type = AppType.MANAGE)
    public Comment submit(@Valid @RequestBody CommentCreateCmd cmd) {
        return commentService.submit(cmd);
    }
}

